package com.tianhui.consignortwofei.mvp.presenter;

import android.content.Context;

import com.fgs.common.CommonResponse;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.mvp.contract.EvaluationContract;
import com.tianhui.consignortwofei.mvp.model.EvaluationModel;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.evaluation.EvaluationInfo;

import java.util.List;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/5/6
 * @description
 **/
public class EvaluationPresenter extends EvaluationContract.Presenter {
    private Context context;
    private EvaluationModel mModel;

    public EvaluationPresenter(Context context) {
        this.context = context;
        mModel = new EvaluationModel();
    }

    @Override
    public void requestEvaluationType(Map<String, String> map,int type, boolean isDialog) {
        mModel.dictionaryModel(context, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DictionaryInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<DictionaryInfo>> baseResponse) {
                if (null != getView()) {
                    if (baseResponse.getCode() == 20000) {
                        List<DictionaryInfo> list = baseResponse.getData();
                        if (null != list && list.size() > 0) {
                            getView().showEvaluationType(type,list);
                        } else {
                            LogUtil.print("获取的评价类型数据为空");
                        }
                    } else {
                        getView().setMsg(baseResponse.getMsg());
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (null != getView()) {
                    getView().setMsg(e.message);
                }
            }
        });
    }


    @Override
    public void requestEvaluationCommit(EvaluationInfo info, boolean isDialog) {
        mModel.evaluationCommitModel(context, info, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse baseResponse) {
                if (null != getView()) {
                    if (baseResponse.getCode() == 20000) {
                       getView().showEvaluationSuccessDialog();
                    } else {
                        getView().setMsg(baseResponse.getMsg());
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (null != getView()) {
                    getView().setMsg(e.message);
                }
            }
        });
    }
}
